home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 418_02 / rasmol2 / mswin31.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-02  |  6.5 KB  |  280 lines

  1. /* mswin31.c
  2.  * RasMol2 Molecular Graphics
  3.  * Roger Sayle, February 1994
  4.  * Version 2.3
  5.  */
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11.  
  12.  
  13. #define GRAPHICS
  14. #include "rasmol.h"
  15. #include "graphics.h"
  16. #include "render.h"
  17.  
  18.  
  19. static BITMAPINFO __far *BitInfo;
  20. static HCURSOR WaitCursor;
  21. static HCURSOR OldCursor;
  22. static int ColCount;
  23.  
  24.  
  25. void AllocateColourMap()
  26. {
  27.     register COLORREF ref;    
  28.     register int i;
  29.     
  30.     if( ColourMap )
  31.         DeleteObject(ColourMap);
  32.         
  33.  
  34.     ColCount = 0;
  35.     for( i=0; i<256; i++ )
  36.         if( ULut[i] ) 
  37.         {  Palette->palPalEntry[ColCount].peFlags = 0;
  38.            Palette->palPalEntry[ColCount].peRed   = RLut[i];
  39.            Palette->palPalEntry[ColCount].peGreen = GLut[i];
  40.            Palette->palPalEntry[ColCount].peBlue  = BLut[i];
  41.  
  42.            BitInfo->bmiColors[ColCount].rgbBlue     = BLut[i];
  43.            BitInfo->bmiColors[ColCount].rgbGreen    = GLut[i];
  44.            BitInfo->bmiColors[ColCount].rgbRed      = RLut[i];
  45.            BitInfo->bmiColors[ColCount].rgbReserved = 0;
  46.            ColCount++;
  47.         }   
  48.     Palette->palNumEntries = ColCount;
  49.     BitInfo->bmiHeader.biClrUsed = ColCount;   
  50.     ColourMap = CreatePalette(Palette);
  51.  
  52.     for( i=0; i<256; i++ )
  53.        if( ULut[i] )
  54.        {   ref = RGB(RLut[i],GLut[i],BLut[i]);
  55.            Lut[i] = GetNearestPaletteIndex(ColourMap,ref);
  56.        }    
  57. }
  58.  
  59.  
  60. void ClearImage()
  61. {
  62.     RECT rect;
  63.     HDC hDC;
  64.     
  65.     hDC = GetDC(CanvWin);
  66.     GetClientRect(CanvWin,&rect);
  67.     FillRect(hDC,&rect,GetStockObject(BLACK_BRUSH));
  68.     ReleaseDC(CanvWin,hDC);
  69.  
  70.     if( PixMap )
  71.     {   DeleteObject(PixMap);
  72.         PixMap = NULL;
  73.     }
  74. }
  75.  
  76.  
  77. void TransferImage()
  78. {
  79.     HPALETTE OldCMap;
  80.     HDC hDC;
  81.         
  82.     if( PixMap )
  83.         DeleteObject(PixMap);
  84.  
  85.     BitInfo->bmiHeader.biWidth = XRange;
  86.     BitInfo->bmiHeader.biHeight = YRange;
  87.         
  88.     hDC = GetDC(NULL);
  89.     FBuffer = (Pixel  __huge*)GlobalLock(FBufHandle);
  90.     /* CreateBitMap(XRange,YRange,1,8,FBuffer); */
  91.  
  92.     if( ColourMap )
  93.     {   OldCMap = SelectPalette(hDC,ColourMap,FALSE);
  94.         RealizePalette(hDC);  /* GDI Bug?? */
  95.     }
  96.         
  97.     PixMap = CreateDIBitmap( hDC, (BITMAPINFOHEADER __far *)BitInfo, 
  98.                              CBM_INIT, FBuffer, BitInfo, DIB_RGB_COLORS);
  99.         
  100.     if( ColourMap )                         
  101.         SelectPalette(hDC,OldCMap,False);
  102.  
  103.     GlobalUnlock(FBufHandle);
  104.     ReleaseDC(NULL,hDC);
  105.     
  106.     InvalidateRect(CanvWin,NULL,FALSE);
  107.     UpdateWindow(CanvWin);
  108. }
  109.  
  110.  
  111. int PrintImage()
  112. {
  113.     register char *device, *driver, *output;
  114.     register int xsize, ysize, xres, yres;
  115.     register int dx, dy, caps;
  116.     char printer[80];
  117.  
  118.     DOCINFO info;
  119.     RECT rect;
  120.     HDC hDC;
  121.  
  122.     GetProfileString("windows","device", "", printer, 80 );
  123.     if( !(device = strtok(printer,",")) ) return( False );
  124.     if( !(driver = strtok((char*)NULL,", ")) ) return( False );
  125.     if( !(output = strtok((char*)NULL,", ")) ) return( False );
  126.  
  127.     hDC = CreateDC(driver,device,output,NULL);
  128.     if( !hDC ) return( False );
  129.  
  130.     caps = GetDeviceCaps( hDC, RASTERCAPS );
  131.     if( !(caps & RC_STRETCHDIB) ) return( False );
  132.     
  133.     xres = GetDeviceCaps( hDC, LOGPIXELSX );
  134.     yres = GetDeviceCaps( hDC, LOGPIXELSY );
  135.     xsize = GetDeviceCaps( hDC, HORZRES );
  136.     ysize = GetDeviceCaps( hDC, VERTRES );
  137.  
  138.     dx = xsize - xres;
  139.     dy = (int)(((long)dx*YRange)/XRange);
  140.  
  141.     /* Should set printer abort procedure */
  142.     /* Position Image on Printed Page */
  143.     rect.top = yres;        rect.bottom = rect.top + dy;
  144.     rect.left = xres>>1;    rect.right = rect.left + dx;
  145.     Escape( hDC, SET_BOUNDS, sizeof(RECT), (char __far*)&rect, NULL );
  146.  
  147.     /* Start RasWin Document */
  148.     info.cbSize = sizeof(DOCINFO);
  149.     info.lpszDocName = "RasWin";
  150.     info.lpszOutput = NULL;
  151.     StartDoc( hDC, &info );
  152.     StartPage( hDC );
  153.     
  154.  
  155.  
  156.     BitInfo->bmiHeader.biWidth = XRange;
  157.     BitInfo->bmiHeader.biHeight = YRange;
  158.     FBuffer = (Pixel  __huge*)GlobalLock(FBufHandle);
  159.  
  160.     StretchDIBits( hDC, xres>>1, yres, dx, dy, 
  161.                         0, 0, XRange, YRange, 
  162.                         FBuffer, BitInfo, DIB_RGB_COLORS, SRCCOPY );
  163.  
  164.     GlobalUnlock(FBufHandle);
  165.  
  166.     EndPage( hDC );
  167.     EndDoc( hDC );
  168.  
  169.     DeleteDC( hDC );
  170.     return( True );
  171. }
  172.  
  173.  
  174. void UpdateScrollBars()
  175. {
  176.     register int pos;
  177.     
  178.     pos = 50-(int)(50.0*DialValue[0]);
  179.     SetScrollPos(CanvWin,SB_VERT,pos,TRUE);
  180.     
  181.     pos = (int)(50.0*DialValue[1])+50;
  182.     SetScrollPos(CanvWin,SB_HORZ,pos,TRUE);
  183. }
  184.  
  185.  
  186. int LookUpColour( name, r, g, b )
  187.     char *name; int *r, *g, *b;
  188. {
  189.     return( False );
  190. }    
  191.  
  192.  
  193. int OpenDisplay( instance, mode )
  194.     HANDLE instance; int mode;
  195. {
  196.     register int i,size;
  197.     long style;
  198.     RECT rect;
  199.  
  200.     PixMap = NULL;
  201.     ColourMap = NULL;
  202.     UseHourGlass = True;
  203.  
  204.     for( i=0; i<8; i++ )
  205.          DialValue[i] = 0.0;
  206.  
  207.  
  208.     RLut[0] = GLut[0] = BLut[0] = 0;
  209.     XRange = CanvWidth;   WRange = XRange>>1;
  210.     YRange = CanvHeight;  HRange = YRange>>1;
  211.     Range = MinFun(XRange,YRange);
  212.     
  213.     rect.top  = 0;   rect.bottom = YRange;
  214.     rect.left = 0;   rect.right  = XRange;
  215.  
  216.     
  217.     style = WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL;
  218.     
  219.     
  220.     AdjustWindowRect(&rect,style,TRUE);
  221.     CanvWin = CreateWindow("RasWinClass","RasMol Version 2.3", style,
  222.                 CW_USEDEFAULT, CW_USEDEFAULT,
  223.                             rect.right-rect.left, 
  224.                             rect.bottom-rect.top,
  225.                             NULL,NULL,instance,NULL);
  226.                             
  227.  
  228.     WaitCursor = LoadCursor(NULL,IDC_WAIT);
  229.  
  230.     size = sizeof(LOGPALETTE) + 256*sizeof(PALETTEENTRY);
  231.     Palette = (LOGPALETTE __far*)_fmalloc( size );
  232.     size = sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD);
  233.     BitInfo = (BITMAPINFO __far*)_fmalloc( size );
  234.  
  235.  
  236.     if( !CanvWin || !Palette || !BitInfo )
  237.         return( False );
  238.         
  239.     Palette->palVersion = 0x300;   
  240.     
  241.     BitInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  242.     BitInfo->bmiHeader.biCompression = BI_RGB;
  243.     BitInfo->bmiHeader.biXPelsPerMeter = 0;
  244.     BitInfo->bmiHeader.biYPelsPerMeter = 0;
  245.     BitInfo->bmiHeader.biClrImportant = 0;
  246.     BitInfo->bmiHeader.biSizeImage = 0;
  247.     BitInfo->bmiHeader.biBitCount = 8;
  248.     BitInfo->bmiHeader.biPlanes = 1;
  249.     
  250.   
  251.     ShowWindow(CanvWin,mode);
  252.     UpdateScrollBars();
  253.     UpdateWindow(CanvWin);
  254.     return(True);                       
  255. }
  256.  
  257.     
  258. void BeginWait()
  259. {
  260.     if( UseHourGlass )
  261.         OldCursor = SetCursor(WaitCursor);
  262. }
  263.  
  264.  
  265. void EndWait()
  266. {
  267.     if( UseHourGlass )
  268.         SetCursor(OldCursor);
  269. }
  270.  
  271.  
  272. void CloseDisplay()
  273. {
  274.     if( ColourMap )
  275.         DeleteObject(ColourMap);
  276.     if( PixMap )
  277.         DeleteObject(PixMap);
  278. }
  279.  
  280.